//
//  YAYIPersoninfoCell.m
//  yayi
//
//  Created by Veer on 16/3/18.
//  Copyright © 2016年 hxp. All rights reserved.
//

#import "YAYIMeHeadCell.h"
#import "YAYICertificationViewController.h"

#import "YAYISettingItemModel.h"
#import "NSString+Tools.h"

@interface YAYIMeHeadCell()

@property (nonatomic, weak) UIImageView *iconVw;
@property (nonatomic, weak) UILabel *nameLbl;
@property (nonatomic, weak) UILabel *typeLbl;
@property (nonatomic, weak) UIButton *verificationSignalBtn;


@end

@implementation YAYIMeHeadCell


- (void)setModel:(YAYISettingItemModel *)model
{
    //[super setItem:item];
    
    NSString *iconUrlStr = [NSString stringWithFormat:@"%@%@", API, model.icon];
    
    [self.iconVw sd_setImageWithURL:[NSURL URLWithString:iconUrlStr] placeholderImage:[UIImage imageNamed:@"doctor"] options:SDWebImageRefreshCached];
    
    
    self.nameLbl.text = model.name;
    self.typeLbl.text = model.detail;
    
    if ([model.signalStr isEqualToString:@"去实名认证"]) {
        self.verificationSignalBtn.enabled = YES;
    }else{
        self.verificationSignalBtn.enabled = NO;
    }
    [self.verificationSignalBtn setTitle:model.signalStr forState:UIControlStateNormal];
    CGSize size = [model.signalStr sizeWithFont:self.verificationSignalBtn.titleLabel.font];
    
    [self.verificationSignalBtn mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(size.width + 12);
    }];
    
    self.verificationSignalBtn.hidden = !model.isShowSignal;

}

+ (instancetype)cellWith:(UITableView *)tableview
{
    static NSString *cellID = @"mecell";
    
    YAYIMeHeadCell *cell = [tableview dequeueReusableCellWithIdentifier:cellID];
    //YAYIPersoninfoCell *cell = [super cellWith:tablevie];
    
    if (nil == cell) {
        cell = [[YAYIMeHeadCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    return cell;
}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
        // 添加icon
        [self setupSubview];
    }
    return self;
}


- (void)setupSubview
{
    //
    UIImageView *iconVw = [[UIImageView alloc] init];
    iconVw.contentMode = UIViewContentModeScaleAspectFill;
    iconVw.image = [UIImage imageNamed:@"doctor"];
    [self.contentView addSubview:iconVw];
    iconVw.layer.masksToBounds = YES;
    self.iconVw = iconVw;
    
    //
    UILabel *nameLbl = [[UILabel alloc] init];
    nameLbl.text = @"张强";
    nameLbl.font = [UIFont systemFontOfSize:17];
    //nameLbl.textColor = [UIColor lightGrayColor];
    [self.contentView addSubview:nameLbl];
    self.nameLbl = nameLbl;
    
    //
    UILabel *typeLbl = [[UILabel alloc] init];
    typeLbl.text = @"主任医生";
    typeLbl.font = [UIFont systemFontOfSize:15];
    typeLbl.textColor = [UIColor lightGrayColor];
    [self.contentView addSubview:typeLbl];
    self.typeLbl = typeLbl;
    
    // authentication
    UIButton *verificationSignalBtn = [[UIButton alloc] init];
    [verificationSignalBtn setTitle:@"去实名认证" forState:UIControlStateNormal];
    [verificationSignalBtn setTitleColor:YAYIColor(104, 174, 240) forState:UIControlStateNormal];
    verificationSignalBtn.titleLabel.font = [UIFont systemFontOfSize:13];
    verificationSignalBtn.layer.cornerRadius = 2;
    verificationSignalBtn.layer.masksToBounds = YES;
    verificationSignalBtn.layer.borderWidth = 1.4;
    verificationSignalBtn.layer.borderColor = YAYIColor(104, 174, 240).CGColor;
    [self.contentView addSubview:verificationSignalBtn];
    self.verificationSignalBtn = verificationSignalBtn;
    
    [verificationSignalBtn addTarget:self action:@selector(onVerifyBtnClick:) forControlEvents:UIControlEventTouchUpInside];
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat margin = 10;
    // 设置frame
    [self.iconVw mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(margin);
        make.bottom.mas_equalTo(-margin);
        make.leading.mas_equalTo(margin * 2);
        make.width.mas_equalTo(self.iconVw.mas_height);
    }];
    self.iconVw.layer.cornerRadius = 35;
    
    
    //
    [self.nameLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(margin);
        make.left.mas_equalTo(self.iconVw.mas_right).mas_offset(margin);
        make.height.mas_equalTo(self.iconVw.mas_height).multipliedBy(0.5);
    }];
    
    // 
    [self.typeLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-margin);
        make.left.mas_equalTo(self.iconVw.mas_right).mas_offset(margin);
        make.height.mas_equalTo(self.iconVw.mas_height).multipliedBy(0.5);
    }];
    
    [self.verificationSignalBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.nameLbl.mas_right).offset(5);
        make.left.mas_greaterThanOrEqualTo(self.typeLbl.mas_right).offset(5);
        make.centerY.equalTo(self.nameLbl);
        make.height.equalTo(@22);
//        make.width.equalTo(@(size.width + 12));
    }];
}


- (void)onVerifyBtnClick:(UIButton *)sender
{
    
    
    UITabBarController *vc = (UITabBarController *)[UIApplication sharedApplication].keyWindow.rootViewController;
    UINavigationController *nav = vc.childViewControllers[2];
    YAYILog(@"认证按钮点击:%@", vc.childViewControllers[2]);
    
    YAYICertificationViewController *verifyVc = [[YAYICertificationViewController alloc] init];
    [nav pushViewController:verifyVc animated:YES];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
